<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Api\UserController;
use App\Http\Controllers\Api\EmployeeController;
use App\Http\Controllers\Api\DepartmentController;
use App\Http\Controllers\Api\PositionController;
use App\Http\Controllers\Api\MenuController;
use App\Http\Controllers\Api\RoleController;
use App\Http\Controllers\Api\PermissionController;
use App\Http\Controllers\Api\MenuAdminController;
use App\Http\Controllers\Api\PermissionAdminController;
use App\Http\Controllers\LogController;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "api" middleware group. Make something great!
|
*/

// 认证相关路由（不需要认证）
use App\Http\Controllers\AuthController;

Route::prefix('auth')->group(function () {
    Route::post('/login', [AuthController::class, 'login']);
    Route::post('/register', [AuthController::class, 'register']);
    Route::post('/logout', [AuthController::class, 'logout'])->middleware('auth:sanctum');
    Route::get('/me', [AuthController::class, 'me'])->middleware('auth:sanctum');
    Route::post('/change-password', [AuthController::class, 'changePassword'])->middleware('auth:sanctum');
});

// 用户管理API（需要认证）
Route::middleware(['auth:sanctum'])->group(function () {
    Route::post('/users/upload-avatar', [UserController::class, 'uploadAvatar']);
    Route::put('/users/preferences', [UserController::class, 'updatePreferences']);
});

// 管理员用户管理API
Route::middleware(['auth:sanctum'])->prefix('users')->group(function () {
    Route::get('/', [UserController::class, 'index']);
    Route::post('/', [UserController::class, 'store']);
    Route::get('/{user}', [UserController::class, 'show']);
    Route::put('/{user}', [UserController::class, 'update']);
    Route::delete('/{user}', [UserController::class, 'destroy']);
});

// 员工管理API
Route::middleware(['auth:sanctum'])->prefix('employees')->group(function () {
    Route::get('/', [EmployeeController::class, 'index']);
    Route::post('/', [EmployeeController::class, 'store']);
    Route::get('/statistics', [EmployeeController::class, 'statistics']);
    Route::get('/{employee}', [EmployeeController::class, 'show']);
    Route::put('/{employee}', [EmployeeController::class, 'update']);
    Route::delete('/{employee}', [EmployeeController::class, 'destroy']);
});

// 部门管理API
Route::middleware(['auth:sanctum'])->prefix('departments')->group(function () {
    Route::get('/tree', [DepartmentController::class, 'tree']);
    Route::get('/', [DepartmentController::class, 'index']);
    Route::post('/', [DepartmentController::class, 'store']);
    Route::get('/statistics', [DepartmentController::class, 'statistics']);
    Route::get('/{department}', [DepartmentController::class, 'show']);
    Route::get('/{department}/employees', [DepartmentController::class, 'employees']);
    Route::put('/{department}', [DepartmentController::class, 'update']);
    Route::put('/{department}/budget', [DepartmentController::class, 'updateBudget']);
    Route::delete('/{department}', [DepartmentController::class, 'destroy']);
});

// 岗位管理API
Route::middleware(['auth:sanctum'])->prefix('positions')->group(function () {
    Route::get('/', [PositionController::class, 'index']);
    Route::post('/', [PositionController::class, 'store']);
    Route::get('/statistics', [PositionController::class, 'statistics']);
    Route::get('/hierarchy', [PositionController::class, 'hierarchy']);
    Route::get('/{position}', [PositionController::class, 'show']);
    Route::get('/{position}/employees', [PositionController::class, 'employees']);
    Route::put('/{position}', [PositionController::class, 'update']);
    Route::put('/{position}/salary', [PositionController::class, 'updateSalary']);
    Route::delete('/{position}', [PositionController::class, 'destroy']);
});

// 文件管理API（如果需要的话，可以添加）
// Route::middleware(['auth:sanctum'])->prefix('files')->group(function () {
//     // 文件上传、下载、删除等API
// });

// 日志管理API
Route::middleware(['auth:sanctum'])->prefix('logs')->group(function () {
    Route::get('/', [LogController::class, 'index']);           // 获取日志列表
    Route::get('/{id}', [LogController::class, 'show']);        // 获取日志详情
    Route::delete('/{id}', [LogController::class, 'destroy']);  // 删除日志
    Route::delete('/batch', [LogController::class, 'batchDestroy']); // 批量删除日志
    Route::get('/statistics', [LogController::class, 'statistics']); // 获取日志统计
    Route::post('/export', [LogController::class, 'export']);   // 导出日志
    Route::post('/cleanup', [LogController::class, 'cleanup']); // 清理过期日志
    Route::get('/modules', [LogController::class, 'getModules']); // 获取模块列表
    Route::get('/levels', [LogController::class, 'getLevels']); // 获取日志级别列表
    Route::get('/operation-types', [LogController::class, 'getOperationTypes']); // 获取操作类型列表
});

// 通知管理API（如果需要的话，可以添加）
// Route::middleware(['auth:sanctum'])->prefix('notifications')->group(function () {
//     // 通知列表、标记已读、删除等API
// });

// 菜单管理API（兼容旧版本）
Route::middleware(['auth:sanctum'])->prefix('menus')->group(function () {
    Route::get('/', [MenuController::class, 'index']);
    Route::get('/tree', [MenuController::class, 'tree']);
    Route::post('/', [MenuController::class, 'store']);
    Route::get('/{menu}', [MenuController::class, 'show']);
    Route::put('/{menu}', [MenuController::class, 'update']);
    Route::delete('/{menu}', [MenuController::class, 'destroy']);
});

// 新版菜单管理API（专业用户）
Route::middleware(['auth:sanctum'])->prefix('admin/menus')->group(function () {
    Route::get('/', [MenuAdminController::class, 'index']);
    Route::post('/', [MenuAdminController::class, 'store']);
    Route::get('/{menu}', [MenuAdminController::class, 'show']);
    Route::put('/{menu}', [MenuAdminController::class, 'update']);
    Route::delete('/{menu}', [MenuAdminController::class, 'destroy']);

    // 功能管理
    Route::get('/{menu}/functions', [MenuAdminController::class, 'getFunctions']);
    Route::post('/{menu}/functions', [MenuAdminController::class, 'addFunction']);
    Route::put('/{menu}/functions/{function}', [MenuAdminController::class, 'updateFunction']);
    Route::delete('/{menu}/functions/{function}', [MenuAdminController::class, 'removeFunction']);
});

// 角色管理API
Route::middleware(['auth:sanctum'])->prefix('roles')->group(function () {
    Route::get('/', [RoleController::class, 'index']);
    Route::post('/', [RoleController::class, 'store']);
    Route::get('/{role}', [RoleController::class, 'show']);
    Route::put('/{role}', [RoleController::class, 'update']);
    Route::delete('/{role}', [RoleController::class, 'destroy']);
    Route::post('/{role}/permissions', [RoleController::class, 'assignPermissions']);
    Route::delete('/{role}/permissions', [RoleController::class, 'removePermissions']);
});

// 权限管理API（兼容旧版本）
Route::middleware(['auth:sanctum'])->prefix('permissions')->group(function () {
    Route::get('/', [PermissionController::class, 'index']);
    Route::get('/user', [PermissionController::class, 'userPermissions']);
    Route::post('/check', [PermissionController::class, 'checkUserPermission']);
    Route::get('/modules', [PermissionController::class, 'getModules']);
    Route::post('/', [PermissionController::class, 'store']);
    Route::get('/{permission}', [PermissionController::class, 'show']);
    Route::put('/{permission}', [PermissionController::class, 'update']);
    Route::delete('/{permission}', [PermissionController::class, 'destroy']);
});

// 新版权限管理API（专业用户 + 非专业用户）
Route::middleware(['auth:sanctum'])->prefix('admin/permissions')->group(function () {
    // 权限节点管理（专业用户）
    Route::get('/nodes', [PermissionAdminController::class, 'getPermissionNodes']);
    Route::post('/nodes', [PermissionAdminController::class, 'createPermissionNode']);
    Route::put('/nodes/{node}', [PermissionAdminController::class, 'updatePermissionNode']);
    Route::delete('/nodes/{node}', [PermissionAdminController::class, 'deletePermissionNode']);

    // 角色权限管理（非专业用户）
    Route::get('/roles/{role}/functions', [PermissionAdminController::class, 'getRoleFunctions']);
    Route::post('/roles/{role}/functions', [PermissionAdminController::class, 'assignFunctionsToRole']);
    Route::delete('/roles/{role}/functions/{function}', [PermissionAdminController::class, 'removeFunctionFromRole']);
    Route::post('/roles/{role}/preview', [PermissionAdminController::class, 'previewRolePermissions']);

    // 用户权限查询
    Route::get('/user', [PermissionAdminController::class, 'getUserPermissions']);
    Route::post('/user/check', [PermissionAdminController::class, 'checkUserFunctionPermission']);
});

// 考勤管理API（如果需要的话，可以添加）
// Route::middleware(['auth:sanctum'])->prefix('attendance')->group(function () {
//     // 考勤记录、统计、审批等API
// });

// 薪资管理API（如果需要的话，可以添加）
// Route::middleware(['auth:sanctum'])->prefix('salary')->group(function () {
//     // 薪资计算、发放、调整等API
// });

// 保险管理API（如果需要的话，可以添加）
// Route::middleware(['auth:sanctum'])->prefix('insurance')->group(function () {
//     // 保险记录、缴纳、理赔等API
// });

// 工伤管理API（如果需要的话，可以添加）
// Route::middleware(['auth:sanctum'])->prefix('work-injury')->group(function () {
//     // 工伤记录、治疗、补偿等API
// });

// 报表管理API（如果需要的话，可以添加）
// Route::middleware(['auth:sanctum'])->prefix('reports')->group(function () {
//     // 部门报表、人员报表、考勤报表等API
// });

// 系统配置API（如果需要的话，可以添加）
// Route::middleware(['auth:sanctum'])->prefix('system')->group(function () {
//     // 系统配置、参数管理等API
// });